package com.example.photo_sharing.page.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import androidx.recyclerview.widget.RecyclerView;

import com.example.photo_sharing.R;
import com.example.photo_sharing.global.App;
import com.example.photo_sharing.tool.Impl.FocusLikeCollectImpl;
import com.example.photo_sharing.tool.Impl.UserInfoSPImpl;
import com.example.photo_sharing.entity.FindListItem;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class listItemAdapter extends RecyclerView.Adapter<listItemAdapter.findViewHolder> {

    private OnItemClickListener onItemClickListener;
    private Context context;
    private ArrayList<FindListItem> goodsEntities = new ArrayList<>();

    private FocusLikeCollectImpl flc;
    private UserInfoSPImpl usp;
    private SharedPreferences sharedPreferences;
    private App app;

    private boolean isLogin;
    private String userId = "0";


    public listItemAdapter(Context context, ArrayList<FindListItem> goodsEntities){
        //System.out.println(goodsEntities.size());
        app = new App();
        this.context = context;
        this.goodsEntities = goodsEntities;
        sharedPreferences = context.getSharedPreferences("userInfo",Context.MODE_PRIVATE);
        usp = new UserInfoSPImpl(sharedPreferences);
        flc = new FocusLikeCollectImpl();
        isLogin = usp.isLogin();
        if(isLogin){
            userId = usp.getUserId();
        }
    }

    @Override
    public listItemAdapter.findViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = View.inflate(context, R.layout.list_item_share,null);
        return new findViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(listItemAdapter.findViewHolder holder, int position) {
        FindListItem data = goodsEntities.get(position);

        holder.SignOfLike = data.getHasLike();
        System.out.println("qweqweqweqweqweqweqwe"+holder.SignOfLike);
        holder.SignOfCollect = data.getHasCollect();

        holder.likeId = data.getLikeId();
        holder.collectId = data.getCollectId();

        //设置点赞的图标
        if(holder.SignOfLike){
            Picasso.get().load(app.getImg_like_true()).into(holder.mItemButton);
        }
        else {
            Picasso.get().load(app.getImg_like_false()).into(holder.mItemButton);
        }

        //设置收藏的图标
        if(holder.SignOfCollect){
            Picasso.get().load(app.getImg_collect_true()).into(holder.mItemButton2);
        }
        else {
            Picasso.get().load(app.getImg_collect_false()).into(holder.mItemButton2);
        }

        holder.mItemName.setText(data.getUsername());
        holder.mItemTitle.setText(data.getTitle());
        holder.mItemNum.setText(data.getLikeNum() != null ? data.getLikeNum().toString() : "0");
        holder.mItemNum2.setText(data.getCollectNum() != null ? data.getCollectNum().toString() : "0");
        Picasso.get().load(data.getImageUrlList().size()!=0 ?data.getImageUrlList().get(0):"1").into(holder.mItemImg);
        Picasso.get().load(app.getImg_p_avatar()).into(holder.avatar);
        holder.mItemContent.setText(data.getContent()+"");

        //点赞的监听事件
        holder.mItemButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println(data.getLikeId());
                System.out.println(data.getCollectId());
                System.out.println("点赞"+holder.SignOfLike);
                System.out.println("收藏"+holder.SignOfLike);
                System.out.println("点赞"+holder.likeId);
                System.out.println("收藏"+holder.collectId);

                if(isLogin){
                    if(holder.SignOfLike){
                        flc.deLike(holder.likeId);
                        app.setDelay(500);
                        Toast.makeText(context,"取消点赞成功",Toast.LENGTH_LONG).show();
                        holder.SignOfLike = flc.setImgDelike(holder.mItemButton,holder.mItemNum);
                        data.setHasLike(false);
                        data.setLikeNum(data.getLikeNum()-1);
                    }
                    else {
                        flc.setLike(data.getId(),userId);
                        app.setDelay(500);
                        Toast.makeText(context,"点赞成功",Toast.LENGTH_LONG).show();
                        holder.SignOfLike = flc.setImgLike(holder.mItemButton,holder.mItemNum);
                        data.setHasLike(true);
                        data.setLikeNum(data.getLikeNum()!=null?data.getLikeNum():0+1);
                    }
                }
                else{
                    Toast.makeText(context,"登陆后才可以点赞",Toast.LENGTH_LONG).show();
                }
            }
        });

        //收藏的监听事件
        holder.mItemButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isLogin){
                    if(holder.SignOfCollect){
                        flc.deCollect(holder.collectId);
                        app.setDelay(500);
                        Toast.makeText(context,"取消收藏成功",Toast.LENGTH_LONG).show();
                        holder.SignOfCollect = flc.setImgDecollect(holder.mItemButton2,holder.mItemNum2);
                        data.setHasCollect(false);
                        data.setLikeNum(data.getLikeNum()-1);
                    }
                    else {
                        flc.setCollect(data.getId(),userId);
                        app.setDelay(500);
                        Toast.makeText(context,"收藏成功",Toast.LENGTH_LONG).show();
                        holder.SignOfCollect = flc.setImgCollect(holder.mItemButton2,holder.mItemNum2);
                        data.setHasCollect(true);
                        data.setCollectNum(data.getCollectNum()+1);
                    }
                }
                else{
                    Toast.makeText(context,"登陆后才可以收藏",Toast.LENGTH_LONG).show();
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return goodsEntities.size();
    }

    class findViewHolder extends RecyclerView.ViewHolder {

        //定义控件
        private ImageView mItemImg;
        private TextView mItemName;
        private TextView mItemTitle;
        private TextView mItemContent;
        private ImageView avatar;
        private ImageButton mItemButton2;
        private TextView mItemNum2;
        private TextView mItemNum;
        private ImageButton mItemButton;

        //定义变量
        private boolean SignOfLike;
        private boolean SignOfCollect;
        private String likeId = "0";
        private String collectId = "0";

        public findViewHolder( View itemView) {
            super(itemView);
            //找到控件
            mItemImg = itemView.findViewById(R.id.item_img);
            mItemName = itemView.findViewById(R.id.item_name);
            mItemTitle = itemView.findViewById(R.id.item_title);
            mItemContent = itemView.findViewById(R.id.item_content);
            avatar = itemView.findViewById(R.id.item_avatar);
            mItemButton2 = itemView.findViewById(R.id.item_Button2);
            mItemNum2 = itemView.findViewById(R.id.item_number2);
            mItemNum = itemView.findViewById(R.id.item_number);
            mItemButton = itemView.findViewById(R.id.item_Button);

            //设置点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (onItemClickListener!=null){

                        onItemClickListener.OnItemClick(v,goodsEntities.get(getLayoutPosition()));
                    }
                }
            });

        }
    }

    //设置点击事件监听器
    public interface OnItemClickListener {
        public void OnItemClick(View view, FindListItem data);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}

